Imports DaveSexton.DocProject.DocSites

Partial Public Class DocSiteSearch
	Inherits System.Web.UI.Page

	Public ReadOnly Property DocSiteFramePath() As String
		Get
			Return DocSiteManager.Settings.DocSiteFramePath
		End Get
	End Property

	Public Property Query() As String
		Get
			Return CType(ViewState("query"), String)
		End Get
		Set(ByVal value As String)
			ViewState("query") = value
		End Set
	End Property

	Private position As Integer

	Protected Function FormatApiName(ByVal apiName As String) As String
		If String.IsNullOrEmpty(apiName) Then
			Return String.Empty
		Else
			Return HttpUtility.HtmlEncode(apiName) & ", "
		End If
	End Function

	Protected Function FormatApiLocation(ByVal apiLocation As String, ByVal virtualPath As String) As String
		If String.IsNullOrEmpty(apiLocation) Then
			Return virtualPath
		Else
			Return HttpUtility.HtmlEncode(apiLocation)
		End If
	End Function

	Protected Function GetNextPosition() As Integer
		position += 1
		Return position + (resultsGridView.PageIndex * resultsGridView.PageSize)
	End Function

	Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
		If Not User.Identity.IsAuthenticated AndAlso Not DocSiteManager.Settings.SearchEnabled Then
			Response.Redirect(DocSiteManager.Settings.DocSiteFramePath)
		End If

		MyBase.OnInit(e)
	End Sub

	Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
		MyBase.OnLoad(e)

		If Not Page.IsPostBack Then
			browseImageButton.Visible = DocSiteManager.Settings.BrowseIndexEnabled OrElse User.Identity.IsAuthenticated

			Dim _query As String = Request.QueryString("q")

			Me.Query = _query

			If String.IsNullOrEmpty(_query) Then
				resultsGridView.Visible = False
				statisticsPanel.Visible = False
				searchHelpPanel.Visible = True
			End If
		End If
	End Sub

	Protected Sub resultsDataSource_Selected(ByVal sender As Object, ByVal e As ObjectDataSourceStatusEventArgs)
		Dim value As ICollection = CType(e.ReturnValue, ICollection)
		Dim format As String = CType(GetLocalResourceObject("resultsCountLabel.CalculatedText"), String)
		Dim count As Integer = 0

		If (Not value Is Nothing) Then
			count = value.Count
		End If

		resultsCountLabel.Text = String.Format(System.Globalization.CultureInfo.CurrentCulture, format, count)
	End Sub

	Protected Sub browseImageButton_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
		DocSiteNavigator.NavigateToBrowseIndex(Query)
	End Sub

End Class